/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viajaconmigo.web.validators;

import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Pablo
 */
@FacesValidator (value="listaFechasValidator")
public class ListaFechasValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) 
            throws ValidatorException {
        //throw new UnsupportedOperationException("Not supported yet.");
        String validar = context.getExternalContext().getRequestParameterMap().get("validar");
        if (validar != null && validar.equals("new")) {
        //if (((Boolean)component.getAttributes().get("validar")).equals(true)){
        //if (component.getId().equals("salvar")){
            List<SelectItem> fechasList = (List<SelectItem>) component.getAttributes().get("fechasList");
            if (fechasList.isEmpty() || fechasList.get(0).getValue().equals(0)){
                FacesMessage message = new FacesMessage();
                message.setSummary("La lista de fechas no puede ser vacía");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(message);
            }
        }
    }   
}
